# _*_ coding: utf-8 _*_
# @Date: 2020/8/22 9:40
# @Author: 刘利 18603013656
# @File: process_email.py
# @Software: PyCharm

import os
import sqlite3

import pandas as pd

from set.setting import Set


class SCV:

    def __init__(self):
        self.con = sqlite3.connect('company.db')
        self.cur = self.con.cursor()
        self.file_name = Set().file_name  # 需要爬取的数据源文件夹名
        self.table = Set().db_table  # 数据库需要处理的表
        try:
            os.makedirs(self.file_name)  # 创建存放目录
        except Exception as e:
            print(e)

        self.start = 1
        self.end = 50
        self.all_num = len(self.cur.execute('''select id from {}'''.format(self.table)).fetchall())



    def process_scv(self):
        while True:
            li = []
            if self.all_num >= self.end:
                self.save_scv(li)
                self.start = self.end
                self.end += 50
            else:
                self.end = self.all_num
                self.save_scv(li)
                break

    def save_scv(self, li):
        try:
            dt = self.cur.execute(
                '''select company, email, tel from {} where id>:start and id<:end'''.format(self.table),
                {'start': self.start, 'end': self.end}).fetchall()

            for item in dt:
                li.append(item)
            print('start:{}, end:{}'.format(self.start, self.end))

            columns = ['name', 'email', 'tel']
            pt = pd.DataFrame(li, columns=columns)
            pt.to_csv(r'{}/{}-{}-{}.csv'.format(self.file_name, self.table, self.start, self.end), index=0)
        except Exception as e:
            print(e)


if __name__ == '__main__':
    scv = SCV()
    scv.process_scv()
